/* ===================================
Michal Kowalczyk
2010 EZI
==================================== */

#include "stdafx.h"
#include "engine.h"
#include "mos3d.h"
#include "processes.h"

void MoveTo::update()
{	
	tx = tmpEntity->getPosition3D().x;
	tz = tmpEntity->getPosition3D().z;

	if ( destx >= tx )
	{
		tx += v * 0.2f;
		if ( tx >= destx)
			xreached = true;
	}
	else if( destx < tx )
	{
		tx -= v * 0.2f;
		if ( tx <= destx)
			xreached = true;
	}

	if ( destz >= tz )
	{
		tz += v * 0.2f;
		if ( tz >= destz)
			zreached = true;
	}
	else if( destz < tz )
	{
		tz -= v * 0.2f;
		if ( tz <= destz)
			zreached = true;
	}

	if( (xreached) && (zreached) )
	{
		isDead = true;
		//STLogger::Instance().log("Destination reached ", m_id, 0);
		if(silentMove)
		{
			EventManager::Instance().sendNewMove(m_id, region);
			currentSkip = 0;
		}
	}

	if(!silentMove)
	{
		if(currentSkip > skips)
		{
			CollisionInfo tmpCollisionInfo = EventManager::Instance().sendActorMoved(m_id);
			currentSkip = 0;
			if ( tmpCollisionInfo.collision )
			{
				isDead = true;
				//STLogger::Instance().log("Collision occur ", m_id, 0);
				EventManager::Instance().sendCollision(m_id, tmpCollisionInfo.withWhat);
			}
		}
		else
			currentSkip += (rand() % 5);
	}

	tmpEntity->setPosition3D(tx, -tz);
}


void waitProcess::update()
{
	m_start += 10;
	if( m_start >= m_stop)
		isDead = true;
}

void jump::update()
{
	if(up)
	{
		if(tmpHeight <= m_height)
			tmpHeight += v * 0.2f;
		else
			up = false;
	}
	else
	{
		if(tmpHeight >= ground)
			tmpHeight -= v * 0.3f;
		else
		{
			if(tmpCount < m_count)
			{
				tmpCount += 1;
				up = true;
			}
			else
				isDead = true;
		}
	}
	tmpEntity->setHeight(tmpHeight);
}